package com.itguigu.rabbitmq.seven;

import com.itguigu.rabbitmq.utils.RabbitMqutil;
import com.rabbitmq.client.CancelCallback;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.DeliverCallback;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

/**
 * @Date 2021/7/19 9:04
 * @Created by wangle
 * @Descriptionn 声明主题交换机及相关队列，
 * 消费者1
 */
public class Receive1 {


    public static final String exchangeName = "topic_logs";

    public static void main(String[] args) throws Exception {

        Channel channel = RabbitMqutil.getChannel();
        //声明交换机
        channel.exchangeDeclare(exchangeName, "topic");
        //声明队列
        String queueName = "Q1";
        channel.queueDeclare(queueName, false, false, false, null);
        channel.queueBind(queueName, exchangeName, "*.orange.*");
        System.out.println("等待接收消息....");

        //声明 接受消息
        DeliverCallback deliverCallback = (consumerTag, message) -> {
            System.out.println(new String(message.getBody()));
            System.out.println("接收队列" + queueName + "绑定键" + message.getEnvelope().getRoutingKey());

        };

        //取消消息时的回调
        CancelCallback cancelCallback = (consumerTag) -> {

            System.out.println("消息消费被中断");
        };
        //接收消息
        channel.basicConsume(queueName, true, deliverCallback, cancelCallback);


    }

}
